diff options
Diffstat (limited to 'src/routes/projects/[id]/+page.server.ts')
| -rw-r--r-- | src/routes/projects/[id]/+page.server.ts | 63 |
1 files changed, 18 insertions, 45 deletions
diff --git a/src/routes/projects/[id]/+page.server.ts b/src/routes/projects/[id]/+page.server.ts index a23d834..66cb9f3 100644 --- a/src/routes/projects/[id]/+page.server.ts +++ b/src/routes/projects/[id]/+page.server.ts @@ -1,26 +1,25 @@ import { error, fail, redirect } from '@sveltejs/kit'; -import { getById } from '$lib/server/services/projects'; +import { + archive as archiveProject, + getById, +} from '$lib/server/services/projects'; import { add, - edit, - archive, - complete, loadProjectDashboard, loadArchived, } from '$lib/server/services/routines'; -import { numericId, parseFormString } from '$lib/server/services/validation'; -import { parseIntervalToDays } from '$lib/shared/routine-metrics'; +import { + archiveRoutineAction, + completeRoutineAction, + editRoutineAction, +} from '$lib/server/services/routine-actions'; +import { + numericId, + parseFormString, + parseFrequencyDays, +} from '$lib/server/services/validation'; import type { Actions, PageServerLoad } from './$types'; -function parseFrequencyDays(value: FormDataEntryValue | null): number { - const raw = value?.toString() ?? ''; - const days = parseIntervalToDays(raw); - if (days === null) { - throw new Error('Invalid interval'); - } - return days; -} - export const load: PageServerLoad = async ({ params }) => { const id = numericId(params.id); const project = await getById(id); @@ -58,47 +57,21 @@ export const actions: Actions = { editRoutine: async ({ request }) => { const data = await request.formData(); - const name = parseFormString(data.get('name')); - if (!name) { - return fail(400, { error: 'Le nom est requis' }); - } - try { - await edit(numericId(data.get('id')), { - name, - frequencyDays: parseFrequencyDays(data.get('frequencyDays')), - }); - } catch { - return fail(400, { error: 'Modification invalide' }); - } - return { success: true }; + return editRoutineAction(data, numericId(data.get('id'))); }, complete: async ({ request }) => { const data = await request.formData(); - try { - await complete( - numericId(data.get('id')), - parseFormString(data.get('note')), - new Date().toISOString(), - ); - } catch { - return fail(400, { error: 'Routine introuvable' }); - } - return { success: true }; + return completeRoutineAction(data, numericId(data.get('id'))); }, archiveRoutine: async ({ request }) => { const data = await request.formData(); - try { - await archive(numericId(data.get('id'))); - } catch { - return fail(400, { error: 'Routine introuvable' }); - } - return { success: true }; + return archiveRoutineAction(numericId(data.get('id'))); }, archiveProject: async ({ params }) => { - await archive(numericId(params.id)); + await archiveProject(numericId(params.id)); throw redirect(303, '/'); }, }; |
